import { dictionary } from './../utils/utils';
/**
 *@auther: tang
 *@Date: 2021-02-22
 *Project: p2pPlatformSys
 *Lang: TypeScript
 */
// 通用接口 实现CRUD
import { Controller } from 'egg';
import * as utils from '../utils/utils';

// 拿到当前的参数

class commonpostController extends Controller {
  async getParams() {
    const { ctx } = this;
    const params:any = ctx.request.body;
    const reqPath = ctx.request.url;
    console.log(ctx.request);
    // 获取字典配置，拿到传递的请求，生成请求参数
    let modelStr = '';
    let state = '';
    // 是否有关联表的对象，格式为[model：模型字符串,{targetKey(从表外键):'',foreignKey(主表外键):''}]
    const related: Array<any> = [];
    // 生成参数
    dictionary.forEach(item => {
      if (item[0] === reqPath) {
        console.log(ctx.request.url, item);
        modelStr = item[2];
        state = item[3];
      }
    });
    // 根据情况生成是否有关联表,在这里配置参数，配置前要到字典utils里面配置参数-----------------------------------------------------------------------
    // -===================================================================================================
    // ========================================================================
    // ===============================================================================
    const base = '/hotwind/';
    switch (reqPath) {
      case base + 'goods/goodsAdd':
        // eslint-disable-next-line no-case-declarations
        if (!params.state) {
          params.state = 0;
        }
        break;
      default:
        break;
    }
    console.log({
      params,
      modelStr,
      state,
      related,
    });
    return {
      params,
      modelStr,
      state,
      related,
      reqPath,
    };
  }
  // 系统菜单资源
  async query() {
    const { ctx } = this;
    const params = await this.getParams();
    // console.log('参数', params);
    const result = await ctx.service.commonpost.query(params.params, params.modelStr, params.related, params.state).catch(() => {
      ctx.body = {
        code: 502,
        msg: '查询失败，服务器异常',
      };
    });
    // console.log(result, modelStr, related, state);
    if (result) {
      console.log(result);
      ctx.helper.success(ctx, '查询成功', {
        row: result.res,
      });
    } else {
      ctx.helper.fail(ctx, {
        code: 400,
        msg: '查询失败，服务器异常',
      });
    }
  }
  async add() {
    const { ctx } = this;
    const params:any = await this.getParams();
    // console.log('参数', params);
    const result = await ctx.service.commonpost.add(params.params, params.modelStr).catch(() => {
      ctx.body = {
        code: 502,
        msg: '添加失败，服务器异常,请联系管理员',
      };
    });
    console.log(result);
    if (result) {
      ctx.body = {
        code: 200,
        msg: '添加成功',
        row: result,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '添加失败，服务器异常',
      };
    }
  }
  async update() {
    const { ctx } = this;
    const params = await this.getParams();
    const reqUrl = ctx.request.url;
    const result = await ctx.service.commonpost.update(params.params, params.modelStr).catch(() => {
      ctx.body = {
        code: 502,
        msg: '修改失败，服务器异常',
      };
    });
    console.log('返回结果', result);
    if (result[0] > 0) {
      if (reqUrl === '/finance/capital/withDrawLogsUpdate' && result.state > -1 && result.state > 1) {
        await utils.hasReadMsg(this, result.mid);
      } else if (reqUrl === '/finance/capital/loanuserdebitlogsUpdate' && result.state > -1 && result.state > 101) {
        await utils.hasReadMsg(this, result.mid);
      }
      ctx.body = {
        code: 200,
        msg: '修改成功',
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '修改失败，服务器异常',
      };
    }
  }
  async delete() {
    const { ctx } = this;
    const params = await this.getParams();
    const result = await ctx.service.commonpost.delete(params.params, params.modelStr).catch(() => {
      ctx.body = {
        code: 502,
        msg: '删除失败，服务器异常',
      };
    });
    if (result) {
      ctx.body = {
        code: 200,
        msg: '删除成功',
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '删除失败，服务器异常',
      };
    }
  }
}

module.exports = commonpostController;
